package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.model.Booking;
import com.bishe.psy.model.ConUser;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.IBookingService;
import com.bishe.psy.service.IStuUserService;
import com.bishe.psy.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-05-02
 * Time: 9:48
 */

@Slf4j
@Api(tags = "预约测试接口")
@RestController
@RequestMapping("/booking")
public class BookingController {

    @Resource
    private IBookingService iBookingService;

    @Resource
    private IStuUserService iStuUserService;


    @ApiOperation("新增预约记录")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "接受者Id") @RequestParam("receiveUserId") Long receiveUserId,
                            @ApiParam(value = "预约时间") @RequestParam("bookingTime") String bookingTime) {

        System.out.println("------------检查是否进如");
        System.out.println(receiveUserId);
        System.out.println(bookingTime);

        //1.参数校验
        if(receiveUserId == null || receiveUserId <= 0 || StringUtil.isEmpty(bookingTime)) {
            return AppResult.failed(ResultCode.FAILED);
        }

        //2.获取用户登录信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //3.查询该用户是否已经预约过并且咨询师还没有确定
        Booking booking = iBookingService.selectByPostUserId(stuUser.getId(), receiveUserId);
        if(booking != null) {
            System.out.println("------------检查是否重复");
            //说明已经预约过，并且咨询师还没有确认
            return AppResult.failed("您已经预约过，请不要重复预约！");
        }

        //4.封装对象
        Booking insertBooking = new Booking();
        insertBooking.setPostUserId(stuUser.getId());
        insertBooking.setReceiveUserId(receiveUserId);
        insertBooking.setBookingTime(bookingTime);

        //5.调用service
        iBookingService.create(insertBooking);


        return AppResult.success();
    }


    //查询某个用户的预约记录
    @ApiOperation("查询某个用户的所有预约记录")
    @GetMapping("/selectAll")
    public AppResult<List<Booking>> selectAll(HttpServletRequest request) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        List<Booking> bookings = iBookingService.selectAll(stuUser.getId());

        //3.返回结果
        return AppResult.success(bookings);
    }


    @ApiOperation("查询某个咨询师的所有以确定预约记录")
    @GetMapping("/selectAllCon")
    public AppResult<List<Booking>> selectAllCon(HttpServletRequest request) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        ConUser conUser = (ConUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        List<Booking> bookings = iBookingService.selectAllCon(conUser.getId());

        //3.返回结果
        return AppResult.success(bookings);
    }



    @ApiOperation("查询咨询师的所有未确定预约记录")
    @GetMapping("/IsNoBooking")
    public AppResult<List<Booking>> selectIsNoBooking(HttpServletRequest request) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        ConUser conUser = (ConUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        List<Booking> bookings = iBookingService.selectIsNoBooking(conUser.getId());

        List<Booking> bookingsResult = new ArrayList<>();

        //3.获取到booking里面的数据
        for(int i = 0; i < bookings.size(); i++) {
            Booking booking = bookings.get(i);
            //查询学生用户是否存在
            StuUser stuUser = iStuUserService.selectByPrimaryKey(booking.getPostUserId());
            if(stuUser.getDeleteState() == 0) {
                bookingsResult.add(booking);
            }
        }

        //3.返回结果
        return AppResult.success(bookingsResult);
    }


    @ApiOperation("修改预约状态")
    @GetMapping("/updateState")
    public AppResult updateState(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED);
        }

        //2.根据id查询预约记录
        Booking booking = iBookingService.selectById(id);
        if(booking == null || booking.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //3.判断state状态是否等于0
        if(booking.getState() == 1) {
            return AppResult.failed("该条记录已确认！");
        }


        //4.封装对象
        Booking updateBooking = new Booking();
        updateBooking.setId(id);
        updateBooking.setState((byte) 1);
        updateBooking.setUpdateState(new Date());

        //5.调用service
        iBookingService.updateStateById(updateBooking);


        return AppResult.success();
    }
}

